/** 
 * 渠道合规审核
 */
import moment from 'moment'
import riskApi from '@/config/server/api/risk'
import orderApi from '@/config/server/api/order'
import dt from '@dt/dt'

// 合作类型
let cooperationType = []
getCooperationType()

function list(params) {
  return riskApi.riskAudit.list.fetch(params).then(res => {
    res.list = res.list.map(i => {
      let cooperationTypeName = ''
      if (cooperationType.find(o => o.key == i.cooperationType)) {
        cooperationTypeName = cooperationType.find(o => o.key == i.cooperationType).value
      }
      return {
        ...i,
        cooperationTypeName,
        orderTapeIds: i.orderTapeIds ? i.orderTapeIds.split(',') : [],
        transfer: dealTransfer(i)
      }
    })
    return res
  })
}

// 查看录音
function tapes(orderTapeIds) {
  return riskApi.riskAudit.tapes.fetch(orderTapeIds).then(res => {
    let data = res.map(i => ({
      ...i,
      createTime: i.createTime ? moment(i.createTime).format('YYYY-MM-DD') : ''
    }))
    return data
  })
}

// 上传录音
function uploadTape({ orderReviewId, tapeUrl, remark }) {
  return riskApi.riskAudit.uploadTape.fetch({
    orderReviewId,
    tapeUrl,
    remark
  }).then(res => {
    return res
  })
}

// 审核
function audit(params) {
  return riskApi.riskAudit.audit.fetch({
    ...params
  }).then(res => {
    return res
  })
}

// 重新提交审核
function reSubmit({ orderReviewId, nodeType }) {
  return riskApi.riskAudit.reSubmit.fetch({
    orderReviewId,
    nodeType
  }).then(res => {
    return res
  })
}

// 修改订单渠道
function updateChannel(params) {
  return orderApi.riskUpdateChannel.fetch({
    ...params
  }).then(res => {
    return res
  })
}

// 查询订单渠道附件（渠道带看、网络获客）
function channelImages(orderReviewId) {
  return riskApi.riskAudit.channelImages.fetch({
    orderReviewId
  }).then(res => {
    return {
      channelTakeLook: res.channelTakeLook ? JSON.parse(res.channelTakeLook) : [],
      networkCertificateImage: res.networkCertificateImage ? JSON.parse(res.networkCertificateImage) : [],
    }
  })
}

// 处理列表传递的日期参数
function dealDate(key, options, index) {
  return options[key] && options[key][index] ? moment(options[key][index]).format('YYYY-MM-DD') : null
}

// 处理流转节点
function dealTransfer(data) {
  let text = ''
  switch (data.divisionState) {
    case '2': text = '子公司综管审核通过'; break;
    case '3': text = '新房事业部审核通过'; break;
    case '4': text = '新房事业部审核驳回'; break;
    default: break;
  }
  if (text == '') {
    switch (data.companyState) {
      case '3': text = '子公司综管审核通过'; break;
      case '4': text = '子公司综管审核驳回'; break;
      default: break;
    }
  }
  if (text == '') {
    text = data.orderCode.indexOf('RG') >= 0 ? '认购单审核通过' : '签约单审核通过'
  }
  return text
}

// 合作类型 list
function getCooperationType() {
  cooperationType = dt.dictInfo().channelType.reduce((arr, { children }) => {
    if (children) {
      for (let item of children) {
        if (!arr.find(i => i.key == item.key)) {
          arr.push({ key: item.key, value: item.value })
        }
      }
    }
    return arr
  }, [{ key: '', value: '无' }])
}

export default {
  list,
  tapes,
  uploadTape,
  audit,
  reSubmit,
  updateChannel,
  channelImages,
  
  dealDate,
}
